package cn.itcast.flink.window;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 *  * 滚动时间窗口案例演示：实时交通卡口流量统计，每隔5秒统计最近5秒钟各个卡口流量
 * @author lilulu
 */
public class WindowProcessDemo {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 2. 数据源-source
        DataStreamSource<String> source = env.socketTextStream("node1", 9999);
        // 3. 数据转换-transformation
        SingleOutputStreamOperator<String> processWindowStream = source.filter(line -> line.trim().split(",").length == 2)
                .map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String line) throws Exception {
                        System.out.println("每条卡口流量数据: " + line);
                        String[] split = line.split(",");
                        return Tuple2.of(split[0], Integer.parseInt(split[1]));
                    }
                }).keyBy(tuple -> tuple.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                    private FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
                    @Override
                    public void process(String key, Context context, Iterable<Tuple2<String, Integer>> iterable, Collector<String> collector) throws Exception {
                        String startTime = this.format.format(context.window().getStart());
                        String endTime = this.format.format(context.window().getEnd());
                        int sum = 0;
                        for (Tuple2<String, Integer> tuple2 : iterable) {
                            sum += tuple2.f1;
                        }
                        String output = "window: [" + startTime + " ~ " + endTime + "], " + key + " = " + sum;
                        collector.collect(output);
                    }
                });
        // 4. 数据终端-sink
        processWindowStream.printToErr();
        // 5. 触发执行-execute
        env.execute("WindowProcessDemo");
    }
}